﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;

namespace Valgmaskine_WebService
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class Service1 : IService1
    {
        // Surftown connection string
        const string _conString = @"Data Source=msdb3.surftown.dk;Initial Catalog=mitoden_dbgr20;User ID=mitoden_dbgr20;Password=dmgruppebjbu";
        SqlConnection con = new SqlConnection(_conString);

        // Localhost connection string
        //const string _conString = @"Data Source=localhost;Initial Catalog=Valgmaskine;Trusted_Connection=True";
        //SqlConnection con = new SqlConnection(_conString);

        bool _threadRunning = true;
        int threadNumber = 0;
        List<int> ValgstedListe = new List<int>();

        public void UpdateWebsiteFromDB()
        {
            if (threadNumber != 1)
            {
            ValgstedListe.Clear();
                threadNumber = 1;
                Thread DBThread = new Thread(ReadDB);
                DBThread.Start();
            }
        }

        private void ReadDB()
        {
            int id = 1;
            while (_threadRunning)
            {
                bool succes = UpdateModelFromDB(id);
                if (succes)
                    id++;
                else
                {
                   
                    Thread.Sleep(500);
                }
                    
            }
        }

        public bool UpdateModelFromDB(int id)
        {
            
            try
            {
                
                int afstemningssted = 0;
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "SELECT seValgSted FROM SenestOpdateretValgsted WHERE seId=@id";
                cmd.Parameters.Add(new SqlParameter("id", id));
                SqlDataReader reader = cmd.ExecuteReader();
                
                while (reader.Read()) // så længe reader læser linjer skal den skrive mere på str
                {
                    if (reader["seValgSted"] != null)
                    {
                        afstemningssted = Convert.ToInt32(reader["seValgSted"]);
                        
                    }
                        
                }
                if (afstemningssted != 0)
                {
                    ValgstedListe.Add(afstemningssted);
                    return true;
                }
                else
                    return false;
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }

        public List<int> ReturnValgsted()
        {
            return ValgstedListe;
        }


    }
}
